// posts/common/video-copy/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    img: {
      type: Object,
      value: {}
    },
    height_data: {
      type: String,
      value: '400rpx'
    },
    width_data: {
      type: String,
      value: '275rpx'
    },
    loop: { // 是否自动播放
      type: Boolean,
      value: true
    },
    video_url: { // 视频路径
      type: String,
      value: ''
    },
    video_pic: { // 默认图片
      type: String,
      value: ''
    },
    title: { // 标题
      type: String,
      value: ''
    },
    fit: {
      type: String,
      value: 'cover'
    },
    topStop: {// 离开顶部距离停止自动播放
      type: Number,
      value: 20
    },
    bottomStop: { // 据顶部距离停止自动播放
      type: Number,
      value: 20
    },
    showFullscreen: {
      type: Boolean,
      value: false
    }
  },
  ready: function () {
    /** 监控视频是否需要播放 */
    //let {screenWidth, screenHeight} = this.extData.systemInfo //获取屏幕高度
    // 压缩视频
    // this.compressVideo(this.properties.video_url)
    var  screenHeight = wx.getSystemInfoSync().windowHeight
    // let topBottomPadding = screenHeight * this.properties.bottomStop / 100;
    let topBottomPadding = screenHeight;
    // let topTopPadding = screenHeight * this.properties.topStop / 100;
    let topTopPadding = screenHeight
    var that = this;
    const videoObserve = this.createIntersectionObserver()
    videoObserve.relativeToViewport({bottom: topBottomPadding, top: topTopPadding})
        .observe(`#video_show`, (res) => {
            let {intersectionRatio} = res
            if(intersectionRatio === 0) {
                //离开视界，因为视窗占比为0，停止播放
                if (that.data.tempFilePath != '') {
                  that.setData({
                    tempFilePath: ''
                  },function () {
                    var context = wx.createVideoContext('video_show',this)
                    context.pause()
                  })
                  // context.play()
                }
            }else{
              if (that.data.tempFilePath=='') {
                that.setData({
                  tempFilePath: that.properties.video_url
                },function () {
                  var context = wx.createVideoContext('video_show',this)
                  context.play()
                })
                // context.play()
              }else {
                var context = wx.createVideoContext('video_show',this)
                context.play()
              }
            }
        })
  },
  /**
   * 组件的初始数据
   */
  data: {
    video_id: 0,
    showVideo: false,
    tempFilePath: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showVideoBind: function () {
      var that = this;
      that.setData({
        showVideo: !that.data.showVideo
      })
    },
    // 压缩视频
    compressVideo: function (url) { console.log('url',url)
    }
  }
})
